// pages/reg/reg.js
let {checkName,reg} = require('../../api/userApi')
Page({
  data:{
    isOk: false,
    info: '',
    userName : '',
    password : '',
  },
  inputUser(ev){
    console.log(ev);
    let {value} = ev.detail;
    this.setData({userName:value});
    checkName(value).then(res => {
      console.log('用户名验证结果:',res.data);
      if(res.data.code) {
        //真：允许注册
        this.setData({isOk:true,info:'允许注册'});
      }else{
        //假：不允许注册
        this.setData({isOk:false,info:'对不起，用户名已存在，请换一个'})
      }
    })
  },
  inputPsw(ev) {
    let {value} = ev.detail;
    this.setData({password:value});
  },
  regBtn(){
    let {isOk} = this.data;
    if(isOk){
      let {password,userName} = this.data;
      if(password && userName){
        reg(userName,password).then(res =>{
          console.log('注册结果', res.data);
          if(res.data.code){
            wx.navigateTo({
              url: '/pages/login/login'
            })
          }
        })
      }else{
        wx.showModal({
          title: '提示',
          content: '请填写完整数据!',
          success (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    }else{
      wx.showModal({
        title: '提示',
        content: '该用户名已存在!',
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  }
})